package com.qidian.mes.defect.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.qidian.mes.defect.service.IncomingService;
import com.qidianit.common.pojo.MESResult;
import com.qidianit.mes.pojo.Detection;
import com.qidianit.mes.pojo.Incoming;
import com.qidianit.mes.pojo.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author 泽华
 * @Description
 * @Time 2024-10-22 14:56
 */
@RestController
@CrossOrigin
@RequestMapping("/incoming")

public class IncomingController {
    @Autowired
    private IncomingService incomingService;

    @GetMapping("/list")
    public MESResult list(Incoming incoming,
                          @RequestParam(defaultValue = "1")int page,
                          @RequestParam(defaultValue = "10") int limit){
        return incomingService.list(incoming,page,limit);
    }

    @PostMapping("/save")
    public MESResult save(@RequestBody Incoming incoming){
        return incomingService.save(incoming);
    }

    @PutMapping("/update")
    public MESResult update(@RequestBody Incoming incoming){
        return incomingService.update(incoming);
    }

    @DeleteMapping("/delete")
    public MESResult delete(Long incomingId){
        return incomingService.delete(incomingId);
    }

    //通过id查询这条数据
    @GetMapping("/get")
    public MESResult get(Long incomingId){
        return incomingService.selectById(incomingId);
    }


    @GetMapping("/export")
    public MESResult export(Incoming incoming, int page, int limit) {
        // 查询数据
        MESResult result = incomingService.list(incoming, 1, 10);
        PageInfo<Incoming> pageInfo = (PageInfo<Incoming>) result.getData();
        List<Incoming> incomings = pageInfo.getList();
        //  通过EasyExcel写数据
        int num = 200;
        try{
            EasyExcel.write("E:\\MES\\Excel\\incoming-"+ page + limit + ".xlsx",
                            Incoming.class)
                    .sheet("来料检验数据")
                    .doWrite(incomings);
        }catch (Exception e){
            num = 300;
        }
        return MESResult.success(num,"OK");
    }



}
